package org.meal.service.impl;

import cn.meal.entity.Merchant;
import cn.meal.mapper.MerchantMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.meal.service.MerchantService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    /**
     * 验证手机号和验证码是否匹配
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean verifyPhoneAndCode(String phone, String code) {
        return true;
    }

    /**
     * 通过手机号获得登录token
     * @param phone
     * @return
     */
    @Override
    public String getTokenByPhone(String phone) {
        // 1.通过手机号查询是否存这个用户
        QueryWrapper<Merchant> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        Merchant merchant=baseMapper.selectOne(wrapper);
        if(merchant==null){
            // 2.添加这个手机号为一个新用户
            merchant=new Merchant();
            merchant.setCreateTime(new Date());
            merchant.setUpdateTime(new Date());
            merchant.setPhone(phone);
            baseMapper.insert(merchant);
            // 3.获得添加后的用户
            merchant=baseMapper.selectOne(wrapper);
        }
        // 3.创建token
        String token=this.createToken(merchant);
        return token;
    }

    /**
     * 创建登录用户的Token(等待实现)
     * @param merchant
     * @return
     */
    private String createToken(Merchant merchant) {
        return "12345";
    }


}
